#!/bin/bash

# Update system packages
sudo apt update -y

# Install Certbot and its dependencies
sudo apt install -y certbot python3-certbot-nginx

# Obtain an SSL certificate and copy the files to /root/
sudo certbot certonly --nginx --non-interactive --agree-tos -d aimaster001.ddnsking.com --email aimaster.ms01@gmail.com --deploy-hook "cp /etc/letsencrypt/live/aimaster001.ddnsking.com/fullchain.pem /root/fullchain.pem && cp /etc/letsencrypt/live/aimaster001.ddnsking.com/privkey.pem /root/privkey.pem && cp /etc/letsencrypt/live/aimaster001.ddnsking.com/chain.pem /root/chain.pem && cp /etc/letsencrypt/live/aimaster001.ddnsking.com/cert.pem /root/cert.pem"

# Set up a cron job for automatic renewal
sudo crontab -l > mycron
echo "0 0,12 * * * /usr/bin/certbot renew --quiet" >> mycron
sudo crontab mycron
rm mycron

echo "SSL certificate installation and configuration complete!"
